const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const NoticeDao = require('../database/NoticeDao')

const router = express.Router();


router.post("/",loginMiddleware,async(req,res)=>{ //addNotice(title, content, noticeType, ifEmergency, childNeedExamine, groupPeopleID, examine_type, fId)
    //解析jwt凭证，校验，提取user,再使用
    let {notice_title, notice_content, notice_type, notice_ifEmergency, notice_childNeedExamine, group_people_id, notice_fID} = req.body;
    try{
        let result = await NoticeDao.addNotice(notice_title, notice_content, notice_type, notice_ifEmergency, notice_childNeedExamine, group_people_id, 0, notice_fID);
        console.log("/Notices添加的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"添加成功",data:result});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"添加失败",data:null});
    }
})

router.delete("/groupPeopleId/:groupPeopleId",loginMiddleware,async(req,res)=>{//删deleteNoticesByGroupPeopleId(groupPeopleId)
    let groupPeopleId=req.params.groupPeopleId;
    try{
        let result = await NoticeDao.deleteNoticesByGroupPeopleId(groupPeopleId);
        console.log("/Notices删除的结果/groupPeopleId/:groupPeopleId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.delete("/:noticeId",loginMiddleware,async(req,res)=>{//删deleteNotice(id)
    let noticeId=req.params.noticeId;
    let n=await NoticeDao.selectNoticesById(noticeId);
    if(!n){
        return res.send({code:5000,msg:'数据不存在',data:null});
    }
    try{
        let result = await NoticeDao.deleteNotice(noticeId);
        console.log("/Notices删除的结果/:noticeId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    }
    catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.delete("/fId/:fId",loginMiddleware,async(req,res)=>{
    let fId=req.params.fId;
    try{
        let result = await NoticeDao.deleteNoticesByFId(fId);
        console.log("/Notices删除的结果/fId/:fId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
    }
})

router.get("/examine", loginMiddleware, async (req, res) => {
    const { pageIndex, groupId } = req.query;

    try {
        const start = 5 * (Number(pageIndex) - 1);
        const pageObj = await NoticeDao.selectNeedExamineNoticesByPage(start, 5, groupId);
        console.log("/Notices查询的结果/examine是:\n" +JSON.stringify(pageObj) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: pageObj });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
});

router.get("/fId/:fId/:gpId", loginMiddleware, async (req, res) => {
    let fId = req.params.fId;
    let gpId = req.params.gpId;
    try {
        let result = await NoticeDao.selectNoticesByFIdAndGpId(fId, gpId);
        console.log("/Notices查询的结果/fId/:fId/:gpId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/groupPeopleId/:groupPeopleId", loginMiddleware, async (req, res) => {
    let groupPeopleId = req.params.groupPeopleId;
    try {
        let result = await NoticeDao.selectNoticesByGroupPeopleId(groupPeopleId);
        console.log("/Notices查询的结果/groupPeopleId/:groupPeopleId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "服务器错误", data: err });
    }
})

router.get("/page", loginMiddleware, async (req, res) => {
    const { pageIndex, noticeTitle, groupPeopleId } = req.query;

    try {
        const start = 5 * (Number(pageIndex) - 1);
        const pageObj = await NoticeDao.selectNoticesByPage(noticeTitle, start, 5, groupPeopleId);
        console.log("/Notices查询的结果/page是:\n" +JSON.stringify(pageObj) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: pageObj });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
});

router.get("/:id", loginMiddleware, async (req, res) => {//selectNoticesById(id)
    let id = req.params.id;
    try {
        let result = await NoticeDao.selectNoticesById(id);
        console.log("/Notices查询的结果/:id是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    } catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "服务器错误", data: err });
    }
});

router.put("/",loginMiddleware,async(req,res)=>{//改updateNotice(id, title, content, ifEmergency)notice_childNeedExamine,examine_type,examine_user_id,reject_reason,
    let notices = req.body;
    let pn=await NoticeDao.selectNoticesById(notices.notice_id);
    if(!pn){
        return res.send({code:5000,msg:'数据不存在',data:null});
    }
    let n = await NoticeDao.updateNotice(notices.notice_id, notices.notice_title, notices.notice_content, notices.notice_ifEmergency, notices.notice_childNeedExamine, notices.examine_type, notices.examine_user_id, notices.reject_reason);
    if(n){
        console.log("/Notices修改的结果/是:\n" +JSON.stringify(n) + "\n————\n");
        res.send({code:2000,msg:"修改成功",data:null});
    }
    else{
        res.send({code:5000,msg:"数据不存在",data:null});
    }
})

module.exports = router;